import os
import json

class N4dChat:
	def __init__(self):
		'''
		Class Constructor: Initializes Messages
		'''
		
		# Define Messages as a List (empty)
		self.Messages=[]
		pass
		
	#def init
	
	def sendMessage(self, ip, username, message):
		'''
		sends a Message to chat from user,ip
		'''
		try:
			# Msg is a register formed by ip, username and message
		
			msg={"ip":ip, "username": username, "message":message}
			self.Messages.append(msg)
		except Exception as e:
			print "except "+str(e)
			return -1
		
	#def sendMessage
	
	def getMessages(self,startMsg):
		'''
		returns messages from startMsg to last message
		'''
		try:
			# Python sublists notation: list[stat:end]
			return(self.Messages[int(startMsg):]);
		except Exception as e:
			print "except "+str(e)
			return -1
		
		return 0
		
	#def getMessages
	
	def saveChat(self, filename):
		try:
			with open("/tmp/"+filename, 'w') as outfile:
				json.dump(self.Messages, outfile)				
		except Exception as e:
			print "except "+str(e)
			return -1
		return 0
	# def saveChat
				
#class N4dChat
